home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / VirtualFile / VirtualFileTests.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  9.2 KB  |  346 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        VirtualFileTests.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #if debug
  15.  
  16. #ifndef __ABSTRACTFILE__
  17. #include "AbstractFile.h"
  18. #endif
  19.  
  20. #ifndef __RAMFILE__
  21. #include "RAMFile.h"
  22. #endif
  23.  
  24. #ifndef __VIRTUALFILE__
  25. #include "VirtualFile.h"
  26. #endif
  27.  
  28. #ifndef __VVFILEMACFILE__
  29. #include "VVFileMacFile.h"
  30. #endif
  31.  
  32. #ifndef __VIRTUALRAMORDISKFILE__
  33. #include "VirtualRamOrDiskFile.h"
  34. #endif
  35.  
  36. #ifndef __VIRTUALMACFILE__
  37. #include "VirtualMacFile.h"
  38. #endif
  39.  
  40. #ifndef __VIRTUALHFSFILE__
  41. #include "VirtualHFSFile.h"
  42. #endif
  43.  
  44. #ifndef __VIRTUALHFSMACFILE__
  45. #include "VirtualHFSMacFile.h"
  46. #endif
  47.  
  48. #ifndef __VIRTUALFOLDER__
  49. #include "VirtualFolder.h"
  50. #endif
  51.  
  52. #ifndef __VIRTUALENCLOSEDBINARYMACFILE__
  53. #include "VirtualEnclosedBinaryMacFile.h"
  54. #endif
  55.  
  56. #ifndef __VAPPLESINGLEMACFILE__
  57. #include "VirtualASMacFile.h"
  58. #endif
  59.  
  60. #ifndef    __FILEOFFILES__
  61. #include "FileOfFiles.h"
  62. #endif
  63.  
  64. #ifndef __FILEINFILE__
  65. #include "FileInFile.h"
  66. #endif
  67.  
  68. #ifndef    __DEBUGGINGGEAR__
  69. #include "DebuggingGear.h"
  70. #endif
  71.  
  72. #ifndef    __DEBUGASSERT__
  73. #include "DebugAssert.h"
  74. #endif
  75.  
  76. #ifndef    __STRING__
  77. #include "String.h"
  78. #endif
  79.  
  80. #ifndef    __THREADUTILITIES__
  81. #include "ThreadUtilities.h"
  82. #endif
  83.  
  84. #ifndef    __BUFFER__
  85. #include "Buffer.h"
  86. #endif
  87.  
  88. #pragma segment VirtualFileTests
  89.  
  90. extern short gBovineTempVRefNum;
  91. extern long gBovineTempDirID;
  92.  
  93. /***********************************|****************************************/
  94.  
  95. Boolean AreSameLength ( const TAbstractFile& a, const TAbstractFile& b )
  96. {
  97.     long al = 0, bl = 0;
  98.     ASSERT_RETURN_ZERO ( a.GetEnd ( al ) == noErr );
  99.     ASSERT_RETURN_ZERO ( b.GetEnd ( bl ) == noErr );
  100.     return ASSERT ( al == bl );
  101. }
  102.  
  103. /***********************************|****************************************/
  104.  
  105. Boolean AreSamePosition ( const TAbstractFile& a, const TAbstractFile& b )
  106. {
  107.     long al = 0, bl = 0;
  108.     ASSERT_RETURN_ZERO ( a.GetPosition ( al ) == noErr );
  109.     ASSERT_RETURN_ZERO ( b.GetPosition ( bl ) == noErr );
  110.     return ASSERT ( al == bl );
  111. }
  112.  
  113. /***********************************|****************************************/
  114.  
  115. Boolean HaveSameContent ( const TAbstractFile& ac, const TAbstractFile& bc )
  116. {
  117.     long ap = 0, bp = 0, al = 0, bl = 0;
  118.  
  119.     ASSERT_RETURN_ZERO ( ac.GetEnd ( al ) == noErr );
  120.     ASSERT_RETURN_ZERO ( bc.GetEnd ( bl ) == noErr );
  121.  
  122.     ASSERT_RETURN_ZERO ( al == bl );
  123.  
  124.     ASSERT_RETURN_ZERO ( ac.GetPosition ( ap ) == noErr );
  125.     ASSERT_RETURN_ZERO ( bc.GetPosition ( bp ) == noErr );
  126.  
  127.     TAbstractFile& a = (TAbstractFile&) ac;
  128.     TAbstractFile& b = (TAbstractFile&) bc;
  129.  
  130.     ASSERT_RETURN_ZERO ( a.SetPosition ( fsFromStart, 0 ) == noErr );
  131.     ASSERT_RETURN_ZERO ( b.SetPosition ( fsFromStart, 0 ) == noErr );
  132.  
  133.     const unsigned long kBufferSize = 1024;
  134.     CBuffer ab ( kBufferSize ), bb ( kBufferSize );
  135.  
  136.     while ( al > 0 )
  137.     {
  138.         long chunkSize = al > kBufferSize ? kBufferSize : al, acs = chunkSize, bcs = chunkSize;
  139.         ASSERT_RETURN_ZERO ( a.ReadDataIgnore ( (void*) ab.GetPhysicalStart (), acs ) == noErr );
  140.         ASSERT_RETURN_ZERO ( acs == chunkSize );
  141.         ASSERT_RETURN_ZERO ( b.ReadDataIgnore ( (void*) bb.GetPhysicalStart (), bcs ) == noErr );
  142.         ASSERT_RETURN_ZERO ( bcs == chunkSize );
  143.         ASSERT_RETURN_ZERO ( ::memcmp ( ab.GetPhysicalStart (), bb.GetPhysicalStart (), (unsigned int) chunkSize ) == 0 );
  144.         al -= chunkSize;
  145.     }
  146.  
  147.     ASSERT_RETURN_ZERO ( a.SetPosition ( fsFromStart, ap ) == noErr );
  148.     ASSERT_RETURN_ZERO ( b.SetPosition ( fsFromStart, bp ) == noErr );
  149.  
  150.     return true;
  151. }
  152.  
  153. /***********************************|****************************************/
  154.  
  155. Boolean AreEqual ( const TAbstractFile& a, const TAbstractFile& b )
  156. {
  157.     return AreSamePosition ( a, b ) && HaveSameContent ( a, b );
  158. }
  159.  
  160. /***********************************|****************************************/
  161.  
  162. Boolean DoesFileMatch ( TAbstractFile& file, const void* data, unsigned long length )
  163. {
  164.     TAbstractFile* compare = new TRamFile;
  165.     ASSERT ( compare->WriteDataIgnore ( data, length ) == noErr );
  166.     Boolean areEqual = ASSERT ( HaveSameContent ( file, *compare ) );
  167.     delete compare;
  168.     return areEqual;
  169. }
  170.  
  171. /***********************************|****************************************/
  172.  
  173. Boolean WriteReadTest ( TAbstractFile& file, long length )
  174. {
  175.     ASSERT_RETURN_ZERO ( file.SetEnd ( 0 ) == noErr );
  176.     ASSERT_RETURN_ZERO ( file.SetPosition ( fsFromStart, 0 ) == noErr );
  177.  
  178.     CBuffer buffer ( &file, length );
  179.     ASSERT_RETURN_ZERO ( file.WriteData ( buffer.GetPhysicalStart (), length ) == noErr );
  180.     ASSERT_RETURN_ZERO ( length == buffer.GetPhysicalLength () );
  181.     ASSERT_RETURN_ZERO ( DoesFileMatch ( file, buffer.GetPhysicalStart (), length ) );
  182.  
  183.     return true;
  184. }
  185.  
  186. /***********************************|****************************************/
  187.  
  188. Boolean EndTest ( TAbstractFile& file, long length )
  189. {
  190.     long end = 0;
  191.     ASSERT_RETURN_ZERO ( file.SetEnd ( length ) == noErr );
  192.     ASSERT_RETURN_ZERO ( file.GetEnd ( end ) == noErr );
  193.     ASSERT_RETURN_ZERO ( end == length );
  194.     return true;
  195. }
  196.  
  197. /***********************************|****************************************/
  198.  
  199. Boolean EndTests ( TAbstractFile& file )
  200. {
  201.     ASSERT_RETURN_ZERO ( EndTest ( file, 0 ) );
  202. //     ASSERT_RETURN_ZERO ( !EndTest ( file, 0x7FFFFFFF ) );
  203. //     ASSERT_RETURN_ZERO ( !EndTest ( file, -1 ) );
  204.     ASSERT_RETURN_ZERO ( EndTest ( file, 10 * 1024 ) );
  205.     return true;
  206. }
  207.  
  208. /***********************************|****************************************/
  209.  
  210. Boolean WriteReadTests ( TAbstractFile& file )
  211. {
  212.     ASSERT_RETURN_ZERO ( WriteReadTest ( file, 0 ) );
  213.     ASSERT_RETURN_ZERO ( WriteReadTest ( file, 512 ) );
  214.     ASSERT_RETURN_ZERO ( WriteReadTest ( file, 256 ) );
  215.     ASSERT_RETURN_ZERO ( WriteReadTest ( file, 1024 ) );
  216.     ASSERT_RETURN_ZERO ( WriteReadTest ( file, 20 * 1024 ) );
  217.     return true;
  218. }
  219.  
  220. /***********************************|****************************************/
  221.  
  222. Boolean PositionTest ( TAbstractFile& file, short mode, long offset )
  223. {
  224.     long length = 0, position = 0, expected = offset, origPosition = 0;
  225.     ASSERT_RETURN_ZERO ( file.GetEnd ( length ) == noErr );
  226.     ASSERT_RETURN_ZERO ( file.GetPosition ( origPosition ) == noErr );
  227.  
  228.     if ( mode == fsFromMark )
  229.         expected += origPosition;
  230.     else if ( mode == fsFromLEOF )
  231.         expected += length;
  232.  
  233.     ASSERT_RETURN_ZERO ( file.SetPosition ( mode, offset ) == noErr );
  234.     ASSERT_RETURN_ZERO ( file.GetPosition ( position ) == noErr );
  235.     ASSERT_RETURN_ZERO ( position == expected );
  236.     ASSERT_RETURN_ZERO ( file.SetPosition ( fsFromStart, origPosition ) == noErr );
  237.  
  238.     return true;
  239. }
  240.  
  241. /***********************************|****************************************/
  242.  
  243. Boolean PositionTests ( TAbstractFile& file )
  244. {
  245.     long length = 0;
  246.     ASSERT_RETURN_ZERO ( file.GetEnd ( length ) == noErr );
  247.  
  248.     ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromStart, 0 ) );
  249. //     ASSERT_RETURN_ZERO ( !PositionTest ( file, fsFromStart, -1 ) );
  250.     ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromLEOF, 0 ) );
  251.  
  252.     if ( length > 0 )
  253.     {
  254.         ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromStart, 1 ) );
  255.         ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromLEOF, -1 ) );
  256.     }
  257.  
  258. //     ASSERT_RETURN_ZERO ( !PositionTest ( file, fsFromLEOF, 1 ) );
  259.  
  260.     if ( length > 1 )
  261.     {
  262.         ASSERT_RETURN_ZERO ( file.SetPosition ( fsFromStart, length / 2 ) == noErr );
  263.         ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromMark, 0 ) );
  264.         ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromMark, -1 ) );
  265.         ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromMark, 1 ) );
  266.     }
  267.  
  268.     return true;
  269. }
  270.  
  271. /***********************************|****************************************/
  272.  
  273. Boolean AbstractFileTest ( TAbstractFile& file )
  274. {
  275.     TRY
  276.     {
  277.         ASSERT_RETURN_ZERO ( EndTests ( file ) );
  278.         ASSERT_RETURN_ZERO ( PositionTests ( file ) );
  279.         ASSERT_RETURN_ZERO ( WriteReadTests ( file ) );
  280.     }
  281.     EXCEPTION
  282.     {
  283.         chris << "\n#### AbstractFileTest Caught Exception" << endl;
  284.         return false;
  285.     }
  286.     ENDEXCEPTION
  287.  
  288.     return true;
  289. }
  290.  
  291. /***********************************|****************************************/
  292.  
  293. Boolean TestThenDelete ( TAbstractFile* file )
  294. {
  295.     ASSERT_RETURN_ZERO ( file != nil );
  296.     ASSERT_RETURN_ZERO ( AbstractFileTest ( *file ) );
  297.     delete file;
  298.     return true;
  299. }
  300.  
  301. /***********************************|****************************************/
  302.  
  303. Boolean OpenTestThenDelete ( TVirtualFile* file )
  304. {
  305.     ASSERT_RETURN_ZERO ( file != nil );
  306.     ASSERT_RETURN_ZERO ( TestThenDelete ( file ) );
  307.     return true;
  308. }
  309.  
  310. /***********************************|****************************************/
  311.  
  312. void VirtualFileTests ()
  313. {
  314.     TRY
  315.     {
  316.         FSSpec spec;
  317.         spec.vRefNum = gBovineTempVRefNum;
  318.         spec.parID = gBovineTempDirID;
  319.  
  320.         ASSERT ( TestThenDelete ( new TRamFile ) );
  321.  
  322.         PLstrcpy ( spec.name, "\pTForkFile.Data" );
  323.         ASSERT ( TestThenDelete ( new TForkFile ( spec, TAbstractFile::kData ) ) );
  324.  
  325.         PLstrcpy ( spec.name, "\pTForkFile.Resource" );
  326.         ASSERT ( TestThenDelete ( new TForkFile ( spec, TAbstractFile::kResource ) ) );
  327.  
  328.         PLstrcpy ( spec.name, "\pTVirtualHFSFile" );
  329.         ASSERT ( OpenTestThenDelete ( new TVirtualHFSFile ( spec ) ) );
  330.  
  331.         ASSERT ( OpenTestThenDelete ( new TVirtualRamOrDiskFile ( TVirtualRamOrDiskFile::kDisk ) ) );
  332.         ASSERT ( OpenTestThenDelete ( new TVirtualRamOrDiskFile ( TVirtualRamOrDiskFile::kMemory ) ) );
  333.  
  334. //         ASSERT ( OpenTestThenDelete ( new TFileOfFiles ) );
  335.     }
  336.     EXCEPTION
  337.     {
  338.         chris << "\n#### VirtualFileTests Caught Exception" << endl;
  339.     }
  340.     ENDEXCEPTION
  341. }
  342.  
  343. /***********************************|****************************************/
  344.  
  345. #endif    // debug
  346.